#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;

int main(int argc, char **argv)
{
  CV_Assert (argc == 3);

  cv::Mat left = cv::imread(argv[1], cv::IMREAD_GRAYSCALE);
  cv::Mat right = cv::imread(argv[2], cv::IMREAD_GRAYSCALE);

  cv::Mat disparity_16s;
#if 0
  cv::StereoBM bm(cv::StereoBM::BASIC_PRESET, 16, 7);
#else
  cv::StereoSGBM bm(0, 16, 3);
#endif
  bm(left, right, disparity_16s);
  double minVal; double maxVal;
  cv::minMaxLoc(disparity_16s, &minVal, &maxVal);
  std::cout << "Min disp: "<< minVal << "Max value" << maxVal << std::endl;
  cv::Mat disparity;
  disparity_16s.convertTo(disparity, CV_8UC1, 255/(maxVal - minVal));

  cv::imshow("left", left);
  cv::imshow("right", right);
  cv::imshow("disparity", disparity);

  cv::waitKey();

  cv::imwrite("disparity.png", disparity);

  return 0;
}
